package com.wangwen.wechatmp.base.impl;

import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.wangwen.wechatmp.base.WxpayService;
import com.wangwen.wechatmp.dao.base.BaseDao;
import com.wangwen.wechatmp.pay.FacePayEntity;
import com.wangwen.wechatmp.pay.FacePayResult;
import com.wangwen.wechatmp.pay.MyMicroPayRequest;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Author: wangwei
 * @Date: 2019/11/30 10:46
 */
@Service("wxpayService")
public class WxpayServiceImpl  implements WxpayService {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    private static String facePayUrl = "https://api.mch.weixin.qq.com/pay/facepay";

    @Autowired
    private WxpayService wxpayService;
    /**
     * 调用 了原生的接口
     * @param facePayEntity
     * @return
     */
    @Override
    public FacePayResult facePay(FacePayEntity facePayEntity) {
        FacePayResult facePayResult = new FacePayResult();

        WxPayService wxPayService =  this.config(facePayEntity.getAppid(),facePayEntity.getMchId(),facePayEntity.getSubAppId(),facePayEntity.getSubMchId());
        try {
            String result = wxPayService.post(facePayUrl, facePayEntity.toXML(), false);
            logger.info("微信刷脸支付返回结果{}", result);
            facePayResult = FacePayResult.fromXML(result, FacePayResult.class);

        } catch (Exception e) {
            e.printStackTrace();
            facePayResult.setErrCode("500");
            facePayResult.setErrCodeDes("接口调用失败！");
        }
        return facePayResult;
    }

    /**
     * 调用 微信包的方法
     * @param wxPayMicropayRequest
     * @return
     */
    @Override
    public WxPayMicropayResult microPay(MyMicroPayRequest wxPayMicropayRequest) {
        WxPayMicropayResult wxPayMicropayResult = new WxPayMicropayResult();

        WxPayService wxPayService =  this.config(wxPayMicropayRequest.getAppid(),wxPayMicropayRequest.getMchId(),wxPayMicropayRequest.getSubAppId(),wxPayMicropayRequest.getSubMchId());
        try {
            wxPayMicropayResult = wxPayService.micropay(wxPayMicropayRequest);
            logger.info("扫码支付返回结果{}", wxPayMicropayResult);

            // 当return_code 和result_code都为SUCCESS的时，支付成功
            if (WxPayConstants.WxpayTradeStatus.SUCCESS.equals(wxPayMicropayResult.getReturnCode()) &&
                    WxPayConstants.WxpayTradeStatus.SUCCESS.equals(wxPayMicropayResult.getResultCode())){
            }
        }catch (WxPayException e){
            e.printStackTrace();
        }
        return wxPayMicropayResult;
    }

    /**
     * 查询订单接口
     * @param request
     * @return
     */
    @Override
    public WxPayOrderQueryResult queryMicroOrder(WxPayOrderQueryRequest request) {
        WxPayOrderQueryResult result = new WxPayOrderQueryResult();

        try {
            WxPayService wxPayService =  this.config(request.getAppid(),request.getMchId(),request.getSubAppId(),request.getSubMchId());
            //设置微信需要的参数
            request.setAppid(request.getAppid());
            request.setMchId(request.getMchId());
            if(StringUtils.isNotBlank(request.getSubMchId())){
                request.setSubMchId(request.getSubMchId());
            }
            if(StringUtils.isNotBlank(request.getSubAppId())){
                request.setSubAppId(request.getSubAppId());
            }
            request.setNonceStr(request.getNonceStr());
            result = wxPayService.queryOrder(request);

        } catch (WxPayException e) {
            logger.error("异常信息",e);
            result.setReturnCode(e.getReturnCode());
            result.setReturnMsg(e.getReturnMsg());
            result.setResultCode(WxPayConstants.ResultCode.FAIL);
            result.setErrCode(e.getErrCode());
            result.setErrCodeDes(e.getErrCodeDes());
        }
        return result;
    }

    @Override
    public WxPayRefundResult refundMoney(WxPayRefundRequest wxPayRefundRequest) {
        WxPayRefundResult wxPayRefundResult = new WxPayRefundResult();
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxPayRefundRequest.getAppid());
        payConfig.setMchId(wxPayRefundRequest.getMchId());
        if (StringUtils.isNotBlank(wxPayRefundRequest.getSubAppId())){
            payConfig.setSubAppId(wxPayRefundRequest.getSubAppId());
        }
        if (StringUtils.isNotBlank(wxPayRefundRequest.getSubMchId())){
            payConfig.setSubMchId(wxPayRefundRequest.getSubMchId());
        }
        //key 定义在代码中
//        payConfig.setMchKey(Constant.KEY);
        payConfig.setKeyPath("classpath:apiclient_cert.p12");
        try {
            payConfig.initSSLContext();
            WxPayService wxPayService = new WxPayServiceImpl();
            wxPayService.setConfig(payConfig);
            wxPayRefundResult = wxPayService.refund(wxPayRefundRequest);

        } catch (WxPayException e) {
            e.printStackTrace();
            wxPayRefundResult.setReturnMsg(e.getReturnMsg());
            wxPayRefundResult.setReturnCode(e.getReturnCode());
        }catch (Exception e){
            wxPayRefundResult.setErrCodeDes("refundMoney 接口发生异常");
            wxPayRefundResult.setReturnCode("FAIL");
        }
        return wxPayRefundResult;
    }


    //  配置 实例化WxpayService 对象
    public WxPayService config(String appid,String mchId,String subAppId,String subMchId){
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(appid);
        payConfig.setMchId(mchId);
//        payConfig.setMchKey(Constant.KEY);
        if (StringUtils.isNotBlank(subAppId)){
            payConfig.setSubAppId(subAppId);
        }
        if (StringUtils.isNotBlank(subMchId)){
            payConfig.setSubMchId(subMchId);
        }
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }


}
